<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\QQconnect;
use Org\Util\Wechatauth;
use Org\Util\SaeTOAuthV2;
//use Org\Util\SaeTClientV2;
header("Content-Type:text/html; charset=utf-8");
class LoginController extends Controller {

    //进入登录页面
    public function login()
    {
//        if(check_login()){
//            $this->redirect('/Home/Index/index');
//        }else
            $this->display();
    }

    //登录验证
    public function loginYz()
    {
        $phone   =   trim(I('post.student_id'));
        $pwd   =    trim(I('post.pwd'));
        $user     =   M('users')->where(array('student_id'=>$phone))->find();
        if(!$user) {
            $this->error("该账号不存在");
        }elseif(md5($pwd) != $user['pwd']){
            $this->error("密码错误");
        }else{
            session('user_id',$user['id']);
            session('pwd',$pwd);
            $data['login_time'] = date('Y-m-d H:i:s');
            cookie("name",$user['student_id']);
            cookie("Username",$user['student_id']);
            cookie('login_time',time(),3600*1);//用户登录有效时间
            session('login_time',time());
            cookie("user_id",$user['id']);
            cookie("pass",$pwd);
            M('users')->where(array('student_id'=>$phone))->save($data);
            $this->redirect('Index/index');
        }

    }

    //进入注册页面
    public function register()
    {
        $this->display();
    }

    //注册操作
    public function regDo(){
        $data   =   I('post.');
        if(empty($data['student_id']) )    $this->error("信息不完整");
        if(empty($data['pwd'])) $this->error("密码不能为空");

        $data['pwd']  =   md5(I('post.pwd'));

        if($data['pwd'] != md5($data['pwd2'])) $this->error("两次密码不一致");

        if($data['student_id']){
            $if_student_id = M('users')->where(array('student_id'=>$data['student_id']))->getField('id');
            if($if_student_id) $this->error("学号已经注册过");
        }
        unset($data['pwd2']);
        $data['add_time']   =   getTime();
        $data['login_time'] =   getTime();
        $insert    =   M('users')->add($data);
        if($insert){

            session('user_id',$insert);
            cookie('user_id',$insert);
            cookie('name',$data['student_id']);
            cookie('pass',$data['pwd']);
            cookie('login_time',time(),3600*1);//用户登录有效时间
            session('login_time',time());
            $this->redirect('/Home/Index/index');
        }else  {
            $this->error("注册失败");
        }

    }

    //进入修改密码页面
    public function password()
    {

        $this->display();
    }

    //修改密码操作
    public function set_pwd()
    {
        if($_POST){
            $data = I('post.');
            $student_id = M('users')->where(array('student_id'=>$data['student_id']))->getField('id');
            if( !$student_id ){
                $this->error("账号不存在");
            }

            if($data['pwd'] != $data['pwd2']){
                $this->error("两次密码不一致");
            }

            $save['pwd'] = md5($data['pwd']);
            $res = M('users')->where(array('student_id'=>$data['student_id']))->save($save);
            if($res !== false){
                $this->success('修改成功,请登录',"/Home/Login/index");
            }
            else{
                $this->error("修改失败");
            }
        }else{
            $student_id  =   I("get.student_id");
            $this->assign("student_id",$student_id);
            $this->display();
        }
    }

    //退出
    public function quit()
    {
        session_destroy();
        cookie('user_id',null);
        cookie('name',null);
        cookie('pass',null);
        cookie('login_time',null);//用户登录有效时间
        $this->redirect('/Home/Login/index');
    }
}

